package jw.webapi.product;


import jw.model.agent.WxAgent;
import jw.model.order.WxShoppingCart;
import jw.model.product.*;
import jw.repository.agent.WxAgentRepository;
import jw.repository.order.WxShoppingCartRepository;
import jw.repository.product.*;
import jw.util.ResponseUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 产品接口
 * Created by 天赐美颜郭德纲 on 2020/01/03.
 */
@Controller
@CrossOrigin
@RequestMapping("wx/goods/")
public class ProductWebApi {

    @Autowired
    private WxProductTypeRepository wxProductTypeRepository;

    @Autowired
    private WxProductRepository wxProductRepository;

    @Autowired
    private WxProductImageRepository wxProductImageRepository;

    @Autowired
    private WxProColSizeRepository wxProColSizeRepository;

    @Autowired
    private WxProductStockRepository wxProductStockRepository;

    @Autowired
    private WxShoppingCartRepository wxShoppingCartRepository;

    @Autowired
    private WxAgentRepository wxAgentRepository;

    /****
     * 产品类型接口
     * @return
     */
    @RequestMapping("productType")
    @ResponseBody
    public Map<String, Object> productType(){
        Map<String, Object> result = new HashMap<>();
        //类型列表
        List<WxProductType> typeList = wxProductTypeRepository.findBySort();
        List<Map<String,Object>> textMaps = new ArrayList<>();
        for (WxProductType type : typeList) {
            Map<String,Object> mapText = new HashedMap();
            mapText.put("id",type.getId());
            mapText.put("icon",type.getIcon());
            mapText.put("name",type.getName());
            textMaps.add(mapText);
        }
        //产品列表
        List<WxProduct>productList = wxProductRepository.findByTypeIdAndState(typeList.get(0).getId(),1);
        List<Map<String, Object>> proList = new ArrayList<>();
        if (productList.size() > 0){
            for (WxProduct product : productList){
                Map productMap = new HashMap();
                productMap.put("name",product.getName());
                productMap.put("id",product.getId());
                productMap.put("productNo",product.getNo());
                productMap.put("price",product.getRetailSale());
                List<WxProductImage>imageList = wxProductImageRepository.findByProductIdAndIsHomePage(product.getId(),1);
                if (imageList.size() > 0){
                    productMap.put("picture",imageList.get(0).getUrl());
                }
                proList.add(productMap);
            }
        }
        result.put("proList",proList);
        result.put("data",textMaps);
        result.put("errMsg", "成功");
        result.put("returnValue", 1);
        return result;
    }


    /****
     * 产品列表接口
     * @return
     */
    @RequestMapping("productList")
    @ResponseBody
    public Map<String, Object> productList(Integer typeId){
        Map<String, Object> result = new HashMap<>();
        List<WxProduct>productList = wxProductRepository.findByTypeIdAndState(typeId,1);
        List<Map<String, Object>> proList = new ArrayList<>();
        if (productList.size() > 0){
            for (WxProduct product : productList){
                Map productMap = new HashMap();
                productMap.put("name",product.getName());
                productMap.put("id",product.getId());
                productMap.put("productNo",product.getNo());
                productMap.put("price",product.getRetailSale());
                List<WxProductImage>imageList = wxProductImageRepository.findByProductIdAndIsHomePage(product.getId(),1);
                if (imageList.size() > 0){
                    productMap.put("picture",imageList.get(0).getUrl());
                }
                proList.add(productMap);
            }
        }
        result.put("data", proList);
        result.put("errMsg", "成功");
        result.put("returnValue", 1);
        return result;
    }

    /****
     * 产品详情接口
     * @return
     */
    @RequestMapping("detail")
    @ResponseBody
    public Object detail(Integer id,String openId){
        Map<String, Object> result = new HashMap<>();
        WxProduct product = wxProductRepository.findOne(id);
        if (product == null){
            result.put("errMsg", "产品不存在");
            result.put("errno", 2);
            return result;
        }
        WxAgent agent = wxAgentRepository.findByOpenId(openId);
        if (agent == null){
            return ResponseUtil.fail(1,"用户不存在");
        }
        Map<String, Object> productMap = new HashMap<>();
        productMap.put("checkedSpecPrice",product.getRetailSale());
        productMap.put("name",product.getName());
        //产品轮播图
        List<WxProductImage> imageOnes = wxProductImageRepository.findByProductIdAndIsHomePage(product.getId(),1);
        List<Map<String, Object>> imageOneList = new ArrayList<>();
        if (imageOnes.size() > 0){
            for (WxProductImage imageOne : imageOnes){
                Map imageMap = new HashMap();
                imageMap.put("id", imageOne.getId());
                imageMap.put("url", imageOne.getUrl());
                imageOneList.add(imageMap);
            }
        }
        productMap.put("imageOneList", imageOneList);
        //产品详情图
        List<WxProductImage>images = wxProductImageRepository.findByProductIdAndIsHomePage(product.getId(),0);
        List<Map<String, Object>> imageList = new ArrayList<>();
        if (images.size() > 0){
            for (WxProductImage image : images){
                Map imageMap = new HashMap();
                imageMap.put("id", image.getId());
                imageMap.put("url", image.getUrl());
                imageList.add(imageMap);
            }
        }
        productMap.put("images", imageList);
        //首页产品图片
        List<WxProductImage> productImages = wxProductImageRepository.findByProductIdAndIsHomePage(product.getId(),1);
        productMap.put("picUrl",productImages.get(0).getUrl());
        //尺码颜色
        List<WxProColSize>colSizeList = wxProColSizeRepository.findByProductNo(product.getNo());
        List<Map<String, Object>> colorSizeList = new ArrayList<>();
        if (colSizeList.size() > 0){
            for (WxProColSize colorSize : colSizeList){
                Map colorMap = new HashMap();
                colorMap.put("color",colorSize.getColor());
                colorMap.put("size",colorSize.getSize());
                colorMap.put("id",colorSize.getId());
                colorMap.put("sku",colorSize.getSku());
                WxProductStock stock = wxProductStockRepository.findBySku(colorSize.getSku());
                if (stock == null){
                    colorMap.put("stock",0);
                } else {
                if (stock.getStock() < 0){
                    colorMap.put("stock",0);
                } else {
                    colorMap.put("stock",stock.getStock());
                    }
                }
                colorSizeList.add(colorMap);
            }
        }

        //购物车数量
        List<WxShoppingCart> cartList=wxShoppingCartRepository.findByAgentId(agent.getId());
        Integer cartCount=0;
        for(WxShoppingCart wxShoppingCart:cartList){
            cartCount+=wxShoppingCart.getCount();
        }
        productMap.put("colorSizeList", colorSizeList);
        productMap.put("cartGoodsCount", cartCount);
        result.put("data", productMap);
        result.put("errMsg", "成功");
        result.put("errno", 0);
        return result;
    }

}
